<?php
class Member_model extends CI_Model {

    function __construct()
    {
        parent::__construct();
    }
	
	function member_register()
	{
		if (getenv('HTTP_X_FORWARDED_FOR')):
			$ip=getenv('HTTP_X_FORWARDED_FOR');
		else:
			$ip=getenv('REMOTE_ADDR');
		endif;

		$username = $this->input->post('username');
		$password = $this->input->post('password');
		$creat_date = date('Y-m-d H:i:s');
		$update_date = date('Y-m-d H:i:s');
		$last_login = date('Y-m-d H:i:s');
		$last_ip_address = $ip;
		$is_newsletter = $this->input->post('is_newsletter');
		
		$member = array(
			'username' => $username,
			'password' => $password,
			'creat_date' => $creat_date,
			'update_date' => $update_date,
			'last_login' => $last_login,
			'last_ip_address' => $last_ip_address,
			'is_newsletter' => $is_newsletter
		);
		
		$this->db->insert('member',$member);
		$member_id = $this->db->insert_id();
		
		$firstname = $this->input->post('firstname');
		$lastname = $this->input->post('lastname');
		$id_card = $this->input->post('id_card');
		$gender = $this->input->post('gender');
			$date = $this->input->post('date');
			$month = $this->input->post('month');
			$year = $this->input->post('year');
		$birthday = $year.'-'.$month.'-'.$date;
		$education = $this->input->post('education');
		$career = $this->input->post('career');
		$income = $this->input->post('income');
		$address = $this->input->post('address');
		$phone = $this->input->post('phone');
		$email = $this->input->post('email');
		
		$member_info = array(
			'member_id' => $member_id,
			'firstname' => $firstname,
			'lastname' => $lastname,
			'id_card' => $id_card,
			'gender' => $gender,
			'birthday' => $birthday,
			'education' => $education,
			'career' => $career,
			'income' => $income,
			'address' => $address,
			'phone' => $phone,
			'email' => $email
		);
		
		$this->db->insert('member_info',$member_info);
	}
	
	function member_edit_profile()
	{
		
		$password = $this->input->post('password');
		$update_date = date('Y-m-d H:i:s');
		$is_newsletter = $this->input->post('is_newsletter');
		
		$member = array(
			'password' => $password,
			'update_date' => $update_date,
			'is_newsletter' => $is_newsletter
		);
		
		$this->db->update('member',$member);
		
		$firstname = $this->input->post('firstname');
		$lastname = $this->input->post('lastname');
		$id_card = $this->input->post('id_card');
		$gender = $this->input->post('gender');
			$date = $this->input->post('date');
			$month = $this->input->post('month');
			$year = $this->input->post('year');
		$birthday = $year.'-'.$month.'-'.$date;
		$education = $this->input->post('education');
		$career = $this->input->post('career');
		$income = $this->input->post('income');
		$address = $this->input->post('address');
		$phone = $this->input->post('phone');
		
		$member_info = array(
			'member_id' => $member_id,
			'firstname' => $firstname,
			'lastname' => $lastname,
			'id_card' => $id_card,
			'gender' => $gender,
			'birthday' => $birthday,
			'education' => $education,
			'career' => $career,
			'income' => $income,
			'address' => $address,
			'phone' => $phone
		);
		
		$this->db->update('member_info',$member_info);
		
	}
	
	function ajax_username_valid()
	{
		$username = $this->input->post('username');
		$old_username = $this->input->post('old_username');
		//Check allow character
		$disallow_character = array('administrator','admin','user','member','username','password','webmaster','webmasters','support','supports','sale','sales','moderator','');
		if(preg_match('/[^a-zA-Z0-9]+/',$username) || in_array($username,$disallow_character)):
			$response =  1;
		endif;
		//Exist Database		
		$this->db->from('member');
		$this->db->where('username',$username);
		if(!empty($old_username)) $this->db->where_not_in('username',$old_username);
		$is_exist = $this->db->count_all_results();
		if(!empty($is_exist)):
			$response =  1;
		endif;
		echo $response;
	}
	
	function ajax_email_valid()
	{
		$email = $this->input->post('email');
		$old_email = $this->input->post('old_email');
		$this->db->from('member_info');
		$this->db->where('email',$email);
		if(!empty($old_email)) $this->db->where_not_in('email',$old_email);		
		$is_exist = $this->db->count_all_results();
		if(!empty($is_exist)):
			echo '1';
		endif;			
	}
	
	function ajax_id_card_valid()
	{
		$pid = $this->input->post('id_card');
		if(strlen($pid) != 13):
		 	for($i=0, $sum=0; $i<12;$i++)
      		$sum += (int)($pid{$i})*(13-$i);
      		if((11-($sum%11))%10 == (int)($pid{12}))
			echo '1';
		endif;
		echo "55555555555555555";
	}
	
	
}

?>